

package sparql;

/* --------------------------Usercode Section------------------------ */

import java_cup.runtime.*;
import rdf_application_2.*;
%%

/* -----------------Options and Declarations Section----------------- */

%public
%class RDF_Lexer

%type java_cup.runtime.Symbol
%line
%column

%cup

/*
  Declarations

  Code between %{ and %}, both of which must be at the beginning of a
  line, will be copied letter to letter into the lexer class source.
  Here you declare member variables and functions that are used inside
  scanner actions.
*/
%{
    /* To create a new java_cup.runtime.Symbol with information about
       the current token, the token will have no value in this
       case. */
    private Symbol symbol(int type) {
        return new Symbol(type, yyline, yycolumn);
    }

    /* Also creates a new java_cup.runtime.Symbol with information
       about the current token, but this object has a value. */
    private Symbol symbol(int type, Object value) {
        return new Symbol(type, yyline, yycolumn, value);
    }
%}


/*
  Macro Declarations

  These declarations are regular expressions that will be used latter
  in the Lexical Rules Section.
*/


/* White space is a line terminator, space, tab, or line feed. */
WhiteSpace     = [\r|\n|\r\n] | [ \t\f]

/* Define Variable*/

dec_variable = "?" [a-zA-Z]+

dec_literal_character = [A-Za-z0-9_\-\. ]
dec_literal =  ("\""{dec_literal_character}{dec_literal_character}*"\"")

dec_pre =  "@" [a-zA-Z]+

dec_pre_def = ({dec_pre}":"{dec_literal_character}+)

dec_number = ({digit}+)

// The rules below have been inspired from
// http://svn.fifesoft.com/viewvc-1.0.5/bin/cgi/viewvc.cgi/RSyntaxTextArea/trunk/src/org/fife/ui/rsyntaxtextarea/modes/CTokenMaker.flex?logsort=cvs&view=markup&root=RSyntaxTextArea&pathrev=12

letter				= [A-Za-z]
letter_or_underscore		= ({letter}|[_])
digit				= [0-9]
url_gen_delim			= ([:\/\?#\[\]@])
url_sub_delim			= ([\!\$&'\(\)\*\+,;=])
url_unreserved			= ({letter_or_underscore}|{digit}|[\-\.\~])
url_character			= ({url_gen_delim}|{url_sub_delim}|{url_unreserved}|[%])
url_characters			= ({url_character}*)
url_end_character		= ([\/\$]|{letter}|{digit})
dec_uri				= (((https?|f(tp|ile))"://"|"www.")({url_characters}{url_end_character})?)

%%
/* ------------------------Lexical Rules Section---------------------- */

/*
   This section contains regular expressions and actions, i.e. Java
   code, that will be executed when the scanner matches the associated
   regular expression. */

   /* YYINITIAL is the state at which the lexer begins scanning.  So
   these regular expressions will only be matched if the scanner is in
   the start state YYINITIAL. */

<YYINITIAL> {

    "*"                {
                         RDF_Application_2View.tokenTxtArea.append("SEL_ALL\n");
                         return symbol(sym.SEL_ALL,new String(yytext()) );
                       }

    ";"                {
                         RDF_Application_2View.tokenTxtArea.append("SEMI\n");
                         return symbol(sym.SEMI, new String(";"));
                       }

    "var:"             { 
                         RDF_Application_2View.tokenTxtArea.append("DEF_VAR\n");
                         return symbol(sym.DEF_VAR, "var:");
                       }

    "triple:"          { 
                         RDF_Application_2View.tokenTxtArea.append("DEF_TRIPLE\n");
                         return symbol(sym.DEF_TRIPLE, "triple:");
                       }

    "optional:"        {
                         RDF_Application_2View.tokenTxtArea.append("DEF_OPTIONAL\n");
                         return symbol(sym.DEF_OPTIONAL, "triple:");
                       }

    "def:"             {
                          RDF_Application_2View.tokenTxtArea.append("DEF_PRE\n");
                          return symbol(sym.DEF_PRE, "def:");
                       }

    "limit:"           {
                          RDF_Application_2View.tokenTxtArea.append("DEF_LIMIT\n");
                          return symbol(sym.DEF_LIMIT, "limit:");
                       }

    "order:"           {
                          RDF_Application_2View.tokenTxtArea.append("DEF_ORDER\n");
                          return symbol(sym.DEF_ORDER, "order:");
                       }

    "<"                {
                          RDF_Application_2View.tokenTxtArea.append("LTHAN\n");
                          return symbol(sym.LTHAN, "<");
                       }

    ">"                {
                          RDF_Application_2View.tokenTxtArea.append("GTHAN\n");
                          return symbol(sym.GTHAN, ">");
                        }

    {dec_variable}     {
                          RDF_Application_2View.tokenTxtArea.append(yytext()+ "\n");
                          return symbol(sym.VARIABLE, new String(yytext()));
                       }

    {dec_uri}          {
                          RDF_Application_2View.tokenTxtArea.append("URI \n");
                          return symbol(sym.URI,new String(yytext()));
                       }

    {dec_literal}      {
                          RDF_Application_2View.tokenTxtArea.append(yytext()+ "\n");
                          return symbol(sym.LITERAL,new String(yytext()));
                       }

    {dec_number}      {
                          RDF_Application_2View.tokenTxtArea.append(yytext()+ "\n");
                          return symbol(sym.NUMBER,new String(yytext()));
                       }

    {dec_pre}          {

                          RDF_Application_2View.tokenTxtArea.append(yytext()+ "\n");
                          return symbol(sym.P_VARIABLE,new String(yytext().substring(1)));
                       }

    {dec_pre_def}      {
                          
                          RDF_Application_2View.tokenTxtArea.append(yytext()+ "\n");
                          return symbol(sym.P_VARIABLE_DEF,new String(yytext().substring(1)));
                       }


    /* Don't do anything if whitespace is found */
    {WhiteSpace}       { }
}


/* No token was found for the input so throw an error.  Print out an
   Illegal character message with the illegal character that was found. */
[^]                    { // throw new Error("Illegal character -->  "+yytext()+" ");
                          RDF_Application_2View.tokenTxtArea.append( "Illegal character -->  "+yytext()+"\n");
                       }
